package com.yhny.lykj.yuhannongye.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.Adapter.ShopInfoAdapter;
import com.yhny.lykj.yuhannongye.Adapter.ShopRightListAdapter;
import com.yhny.lykj.yuhannongye.MyApplication;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.app.BaseRefreshListAdapter;
import com.yhny.lykj.yuhannongye.app.LSCGridLayoutManager;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.model.ShopListModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * 搜索
 */
public class SearchActivity extends BaseActivity implements BaseRefreshListAdapter.OnItemClickListener {


    private EditText keyEt;
    private RelativeLayout listRl;

    private View tipView;
    private SwipeRefreshLayout refreshLayoutView;
    private RecyclerView.LayoutManager layoutManager;
    private ShopInfoAdapter adapter;
    private ArrayList<ShopListModel.ListEntity> dataList = new ArrayList<>();
    private int lastVisibleItem = 0;
    private boolean isLoadMore = true;
    private int currentPage = 1;
    private boolean isFirstLoad = true;
    private RecyclerView recyclerView;
    private String key;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);


        findViewById(R.id.search_submit).setOnClickListener(this);
        findViewById(R.id.left_img).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);

        keyEt = (EditText) findViewById(R.id.search_value);
        listRl = (RelativeLayout) findViewById(R.id.refresh_list);
        isSearch(true);

        setView();
    }


    /**
     * 是否显示搜索结果
     * @param isShow true 无搜索结果  false 有搜索结果
     */
    private void isSearch(Boolean isShow){
        if (isShow){
            listRl.setVisibility(View.GONE);
        }else{
            listRl.setVisibility(View.VISIBLE);
        }
    }

    private void loadData(int page) {
        currentPage = page;
        if (isFirstLoad) {
            isFirstLoad = false;
            showProgressDialog(R.string.text_loading);
        }

        DataManager.getData(MODEL_PROCESS_0, NetHelper.Shop.keywordssearch(key,currentPage), this);
    }

    private void setView(){
        tipView = findViewById(R.id.tip);
        refreshLayoutView = (SwipeRefreshLayout) findViewById(R.id.refresh);
        refreshLayoutView.setRefreshing(true);
        recyclerView = (RecyclerView) findViewById(R.id.list);
        resetLayoutManagerAndAdapter();
        refreshLayoutView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isLoadMore = true;
                loadData(1);
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (layoutManager instanceof LinearLayoutManager) {
                    lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (isLoadMore && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 >= adapter.getItemCount()) {
                    loadData(currentPage + 1);
                    LSCToast.show(MyApplication.getContext(), R.string.text_load_more);
                }
            }
        });
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).sizeResId(R.dimen.item_vertical_margin).colorResId(R.color.white).build());
    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)||model==null) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
                case MODEL_PROCESS_0: {
                    //店铺
                    BaseModel baseModel = (BaseModel) model;
                    if (isDataEmpty(baseModel)) {
                        if (currentPage == 1) {
                            showNoDataMsg(true);
                        } else {
                            isLoadMore = false;
                            showNoDataMsg(false);
                        }
                        isSearch(false);
                    } else {
                        ShopListModel modelDataList = (ShopListModel) model;
                        if (modelDataList == null || modelDataList.data.isEmpty()) {
                            if (currentPage == 1) {
                                showNoDataMsg(true);
                            } else {
                                isLoadMore = false;
                                showNoDataMsg(false);
                            }
                        } else {
                            if (currentPage == 1) {
                                dataList.clear();
                            }
                            dataList.addAll(modelDataList.data);
                            adapter.notifyDataSetChanged();
                            showNoDataMsg(false);
                        }
                        isSearch(false);
                    }

                    refreshLayoutView.setRefreshing(false);
                    dismissProgressDialog();
                    return true;
                }
        }
        dismissProgressDialog();
        return false;
    }

    private void resetLayoutManagerAndAdapter() {
            layoutManager = getLayoutManager();
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ShopInfoAdapter(dataList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);

    }
    private RecyclerView.LayoutManager getLayoutManager() {
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        final LSCGridLayoutManager layoutManager1 = new LSCGridLayoutManager(this,2);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        return layoutManager1;
    }
    private void showNoDataMsg(boolean isShow) {
        tipView.setVisibility(isShow ? View.VISIBLE : View.GONE);
        refreshLayoutView.setVisibility(isShow ? View.GONE : View.VISIBLE);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_img:{
                //返回
                finish();
                break;
            }
            case R.id.cancel:{
                //取消
                keyEt.setText("");
                isSearch(true);
                break;
            }
            case R.id.search_submit:{
                //搜索
                key = keyEt.getText().toString();
                if (key.isEmpty()){
                    LSCToast.show(this,"请输入搜索关键字！");
                    return;
                }
                loadData(1);
                break;
            }

        }
        super.onClick(v);
    }

    @Override
    public void onItemClick(int position) {

            Intent intent = new Intent(SearchActivity.this,ShopListActivity.class);
        intent.putExtra(SHOP_FROM,SHOP_LIST);
        intent.putExtra(SHOP_ID,dataList.get(position).goodsid);
            startActivity(intent);

    }

}
